package org.vacoor.nothing.misc.express.usps.api.support;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.Locale;

/**
 * @author vacoor
 */
public class BooleanAdapter extends XmlAdapter<String, Boolean> {

    @Override
    public Boolean unmarshal(String value) throws Exception {
        if (null == value) {
            return null;
        }
        value = value.toUpperCase(Locale.ENGLISH);
        if ("TRUE".equals(value) || "1".equals(value) || "Y".equals(value)) {
            return true;
        }
        if ("FALSE".equals(value) || "0".equals(value) || "N".equals(value)) {
            return false;
        }
        return null;
    }

    @Override
    public String marshal(Boolean value) throws Exception {
        return null == value ? "" : value.toString().toUpperCase(Locale.ENGLISH);
    }
}
